<#import "../../common/layout.ftl" as l>


<@l.content>

<script>
    var crud = {
//        read: "/manage/ship/detail",
//        update: '/manage/ship/update',
//        del: "/manage/ship/delete"
    };
    var model = [
//        {name : 'id',type : 'hidden',text : '地理点编号',allowBlank : true,primaryKey : true},
//        {name : 'name',type : 'text',text : '地理点名称',allowBlank : false,primaryKey : false},
//        {name : 'shipEnName',type : 'text',text : '船舶英文名',allowBlank : false,primaryKey : false},
//        {name : 'callSign',type : 'text',text : '船舶呼号',allowBlank : false,primaryKey : false},
//        {name : 'nation',type : 'text',text : '国籍',allowBlank : false,primaryKey : false},
//        {name : 'tonnage',type : 'number',text : '吨位',allowBlank : false,primaryKey : false},
//        {name : 'power',type : 'number',text : '主机功率',value:0,allowBlank : false,primaryKey : false},
//        {name : 'shipLength',type : 'number',text : '船舶长度',allowBlank : false,primaryKey : false},
//        {name : 'draftAfter',type : 'number',text : '后吃水',allowBlank : false,primaryKey : false},
//        {name : 'draftMax',type : 'number',text : '最大吃水',allowBlank : false,primaryKey : false},
//        {name : 'draftForward',type : 'number',text : '前吃水',allowBlank : false,primaryKey : false}
    ];



</script>


<form id="pagination-form" method="post" action="/manage/map/server-point/list">

    <input type="hidden" name="page" value="${pageInfo.pageNum}">
    <input type="hidden" name="limit" value="${pageInfo.pageSize}">
    <input type="hidden" name="totalPage" value="${pageInfo.pages}">

    <div class="ibox">
        <div class="ibox-title">
            <h3><#if Session['currentMenu']??>${Session['currentMenu'].name}<#else></#if></h3>

            <div class="ibox-tools">
                <a onclick="publishServer()"  href="#" id="add-server">
                    <i class="fa fa-plus"></i>
                    添加服务点
                </a>
                <#--<a id="edit" href="#">-->
                    <#--<i class="fa fa-edit"></i>-->
                    <#--编辑-->
                <#--</a>-->

                <#--<a id="pagination-delete" href="#">-->
                    <#--<i class="fa fa-trash"></i>-->
                    <#--删除-->
                <#--</a>-->

            </div>
        </div>
        <div class="ibox-content">
            <div class="input-group search-content">
                <input type="text" name="key" value="<#if key??>${key}<#else></#if>" class="form-control" placeholder="请输入服务名称或关键字"> <span class="input-group-btn">
                <input type="submit" class="btn btn-primary" value="搜索">
            </span></div>
            <div>

                <div class="table-responsive">
                    <table class="table table-striped table-hover dataTables-example" >
                        <thead>
                        <tr>
                            <#--<th>&nbsp;</th>-->
                            <th>名称</th>
                            <th>位置</th>
                            <th>服务类型</th>
                            <th>服务内容</th>
                            <th>服务状态</th>
                            <th>关键字</th>
                            <th>更多</th>
                            <th>操作</th>
                        </tr>
                        </thead>

                        <tbody>
                            <#list pageInfo.list as report>
                            <tr>
                                <#--<td><input type="checkbox" class="i-checks" value="<#if report.id??>${report.id}<#else></#if>"></td>-->
                                <td><#if report.name??>${report.name}<#else></#if></td>
                                <td><#if report.wkt??>${report.wkt}<#else></#if></td>
                                <td><#if report.serverType??>${report.serverType}<#else></#if></td>
                                <td style="width:20%;word-break:break-all"><#if report.message??>${report.message}<#else></#if></td>
                                <td><#if report.status=="0">待审核<#elseif report.status=="1">已发布<#elseif report.status=="2">未通过<#elseif report.status=="-1">未发布</#if></td>
                                <td><#if report.keyword??>${report.keyword}<#else></#if></td>
                                <td><#if report.link??><a target="_blank"  onclick="a('${report.link}')">${report.link}</a><#else></#if></td>
                                <td>
                                    <a onclick="deleteServer('${report.id}')" href="#">删除</a>
                                    <#if report.status=="-1"||report.status=="2">&nbsp;|&nbsp;<a onclick="publishServer('${report.id}')" href="#">编辑&nbsp;|&nbsp;
                                        <a onclick="publish('${report.id}')" href="#">发布</a>
                                    <#elseif report.status=="1">&nbsp;|&nbsp;<a onclick="drop('${report.id}')" href="#">作废</#if>
                                </td>
                            </tr>
                            </#list>
                        </tbody>
                    </table>
                </div>
                <div class="pagination-content">
                    <div id="pagination" class="pagination">
                    </div>
                </div>
            </div>
        </div>

    </div>
</form>


</@l.content>

<script src="/pagination/js/pagination.js"></script>
<script>
    function a(link) {
        window.open(link);
    }


    function publish(id) {
        layer.confirm('是否确认发布该服务点？', {
            btn: ['确认','取消'] //按钮
        }, function(){
            $.ajax({
                type: "POST",
                url: "/manage/map/server-point/save",
                data: {id:id,status:'0'},
                dataType: "json",
                success: function (data) {
                    if (data.result == true) {
                        layer.msg("发布成功");
                        setTimeout(function(){location.reload()},500);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log(errorThrown);
                }
            });
        }, function(){
        });
    }

    function drop(id) {
        layer.confirm('是否确认作废该服务点？', {
            btn: ['确认','取消'] //按钮
        }, function(){
            $.ajax({
                type: "POST",
                url: "/manage/map/server-point/save",
                data: {id:id,status:'-1'},
                dataType: "json",
                success: function (data) {
                    if (data.result == true) {
                        layer.msg("提交成功");
                        setTimeout(function(){location.reload()},500);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log(errorThrown);
                }
            });
        }, function(){
        });
    }



    function deleteServer(id) {
    layer.confirm('是否确认删除该服务点？', {
        btn: ['确认','取消'] //按钮
    }, function(){
        $.ajax({
            url:"/manage/map/server-point/delete",
            type:"POST",
            data:{id:id},
            success:function (data) {
                if(data){
                    layer.msg("删除成功");
                    setTimeout(function () {
                        location.reload();
                    },500);
                }else{
                    layer.msg("删除失败");
                }
            }
        })
    }, function(){

    });
    }

    function type() {
        var str = "";
        $.ajax({
            type: "POST",
            async:false,
            url: "/manage/map/type/selecttype",
            data: {},
            dataType: "json",
            success: function (data) {
                for (var i = 0; i < data.length; i++) {
                    str = str + "<option value='" + data[i].typevalue + "'>" + data[i].typetext + "</option>";
                }
            }
        });
        return str;
    }

    function publishServer(id) {
         var ss= type();
         var info="<div class='form-group'>" +
                 "<label for='name' class='col-sm-3 control-label'>名称</label>" +
                 "<div class='col-sm-9'>" +
                 "<input class='form-control' id='name' type='text'/>" +
                 "</div>" +
                 "</div>";
         info += "<div class='form-group'>" +
                 "<label class='col-sm-3 control-label'>经纬度</label>" +
                 "<div class='col-sm-4'>" +
                 "<input class='form-control' id='lon' type='text' placeholder='经度'/>" +
                 "</div>" +
                 "<div class='col-sm-4'>" +
                 "<input class='form-control' id='lat' type='text' placeholder='纬度'/>" +
                 "</div>" +
                 "</div>" ;
         info += " <div class='form-group'>" +
                         "<label for='selectserver' class='col-sm-3 control-label'>服务类型</label>" +
                         "<div class='col-sm-9'>" +
                            "<select class='form-control' name='selectserver' id='selectserver'>" +
                                ss +
                             "</select>" +
                         "</div>" +
                     "</div>";
         info += "<div class='form-group'>" +
                     "<label for='keyword' class='col-sm-3 control-label'>关键字</label>" +
                     "<div class='col-sm-9'>" +
                     "<input class='form-control' id='keyword' type='text' placeholder='逗号隔开，最多五个字符，最多三个'/>" +
                     "</div>" +
                 "</div>"
         info += "<div class='form-group'>" +
                     "<label for='txtContent' class='col-sm-3 control-label'>服务内容</label>" +
                     "<div class='col-sm-9'>" +
                     "<textarea class='form-control'  id='txtContent' rows='5' ></textarea>" +
                     "</div>" +
                 "</div>";
        info += "<div class='form-group'>" +
                "<label class='col-sm-3 control-label' for='link'>链接</label>" +
                "<div class='col-sm-9'>" +
                "<input class='form-control' id='link' type='text'/>" +
                "</div>" +
                "</div>" ;

      var index= layer.open({
            type: 1, //page层
            area: ['400px','600px'],
            title: '添加服务点',
            shade: 0, //遮罩透明度
            moveType: 1, //拖拽风格，0是默认，1是传统拖动
            shift: 1, //0-6的动画形式，-1不开启
            maxmin: true, //开启最大化最小化按钮
            content: '<form class="form-horizontal" style="padding: 20px;" data-parsley-validate novalidate>'
                       + info +
                    '</form>',
            btn: ['保存','发布','关闭'] //只是为了演示
            ,success:function () {
              if(id)
              {
                  $.ajax({
                      type: "POST",
                      url: "/manage/map/server-point/ajax/getById",
                      data: {id:id},
                      dataType: "json",
                      success: function (data) {
                       var str=data.coordinates.substring(6,data.coordinates.length-1).split(" ");
                          $("#lon").val(str[0]);
                          $("#lat").val(str[1]);
                          $("#selectserver").find("option:selected").text(data.serverType);
                          $("#keyword").val(data.keyword);
                          $("#txtContent").val(data.message);
                          $("#name").val(data.name);
                          $("#link").val(data.link);

                      },
                      error: function (XMLHttpRequest, textStatus, errorThrown) {
                          console.log(errorThrown);
                      }
                  });
              }
          }
            , yes: function(){
                var serverType = $("#selectserver").find("option:selected").text();
                var keyword = $("#keyword").val();
                var message = $("#txtContent").val();
                var wkt='POINT('+$("#lon").val()+' '+$("#lat").val()+')';
                var name=$("#name").val();
                var link=$("#link").val();
                var   prop= {
                    serverType: serverType,
                    keyword: keyword,
                    message: message,
                    wkt:wkt,
                    name:name,
                    status:'-1',
                    link:link
                }
              if(id!==''&&id!==null)
              {

                  prop= {
                      id:id,
                      serverType: serverType,
                      keyword: keyword,
                      message: message,
                      wkt:wkt,
                      name:name,
                      status:'-1',
                      link:link
                  }
              }
                if(name==""){
                    layer.msg("名字不能为空");
                }else if (serverType == "" || message == "" || serverType == "==请选择==") {
                    layer.msg("类型和内容不能为空");
                }else if ($("#lon").val()==""||$("#lat").val()==""){
                    layer.msg("经纬度不能为空");
                }else {
                    $.ajax({
                        type: "POST",
                        url: "/manage/map/server-point/save",
                        data: prop,
                        dataType: "json",
                        success: function (data) {
                            if (data.result == true) {
                                layer.msg("保存成功");
                                layer.close(index);
                                setTimeout(function(){location.reload()},1000);
                            }else {
                                if(data.keyword == false){
                                    layer.msg("关键字过长");
                                }else {
                                    layer.msg("保存失败");
                                }
                            }
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            console.log(errorThrown);
                        }
                    });
                }
            },btn2:  function(){
              var serverType = $("#selectserver").find("option:selected").text();
              var keyword = $("#keyword").val();
              var message = $("#txtContent").val();
              var wkt='POINT('+$("#lon").val()+' '+$("#lat").val()+')';
              var name=$("#name").val();
              var link=$("#link").val();
              var   prop= {
                  serverType: serverType,
                  keyword: keyword,
                  message: message,
                  wkt:wkt,
                  name:name,
                  status:'0',
                  link:link
              }
              if(id!==''&&id!==null)
              {
                  prop= {
                      id:id,
                      serverType: serverType,
                      keyword: keyword,
                      message: message,
                      wkt:wkt,
                      name:name,
                      status:'0',
                      link:link
                  }
              }
              if(name==""){
                  layer.msg("名字不能为空");
              }else if (serverType == "" || message == "" || serverType == "==请选择==") {
                  layer.msg("类型和内容不能为空");
              }else if ($("#lon").val()==""||$("#lat").val()==""){
                  layer.msg("经纬度不能为空");
              }else {
                  $.ajax({
                      type: "POST",
                      url: "/manage/map/server-point/save",
                      data: prop,
                      dataType: "json",
                      success: function (data) {
                          if (data.result == true) {
                              layer.msg("发布成功");
                              layer.close(index);
                              setTimeout(function(){location.reload()},1000);
                          }else {
                              if(data.keyword == false){
                                  layer.msg("关键字过长");
                              }else {
                                  layer.msg("发布失败");
                              }
                          }
                      },
                      error: function (XMLHttpRequest, textStatus, errorThrown) {
                          console.log(errorThrown);
                      }
                  });
              }
              return false;
          }
            ,btn3: function(){
                layer.closeAll();
            }
        });
    }
</script>